import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store' // 引入store

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: 'Dashboard00', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    path: '/dict/data/',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/dict/data/:id?',
        component: () => import('@/views/system/dict/data'),
        name: 'DictData',
        meta: { title: '字典数据明细', icon: 'dict', noCache: false }
      }
    ]
  },
  // 2024-12-23新开发路由
  {
    path: '/currentClass',
    component: () => import('@/views/currentClass/class'),
    hidden: true
  },
  {
    path: '/classAnalysis',
    component: () => import('@/views/actionAnalysis/classAnalysis'),
    hidden: true
  },
  {
    path: '/',
    redirect: '/startClass',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/startClass',
        component: () => import('@/views/currentClass/start'),
        name: 'startClass',
        meta: { title: '行为分析', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/schedule',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/schedule',
        component: () => import('@/views/schedule/index'),
        name: 'scheduleList',
        meta: { title: '定时任务' }
      }
    ]
  },
  {
    path: '/uploadVideo',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/uploadVideo',
        component: () => import('@/views/uploadVideo/index'),
        name: 'uploadVideo',
        meta: { title: '上传视频' }
      }
    ]
  },
  {
    path: '/template',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/template',
        component: () => import('@/views/template/index'),
        name: 'templateMain',
        meta: { title: '模板' }
      }
    ]
  },
  {
    path: '/classList',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/classList',
        component: () => import('@/views/actionAnalysis/classList'),
        name: 'classList'
      }
    ]
  },
  {
    path: '/userList',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/userList',
        component: () => import('@/views/system/user/index.vue'),
        name: 'User'
      }
    ]
  },
  {
    path: '/department',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/department',
        component: () => import('@/views/system/department/index.vue'),
        name: 'Department'
      }
    ]
  },
  {
    path: '/systemInfo',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/systemInfo',
        component: () => import('@/views/monitor/server/index.vue'),
        name: 'systemInfo'
      }
    ]
  },
  {
    path: '/authorize', // 授权管理页（始终可访问）
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/authorize',
        component: () => import('@/views/authorize/index.vue'),
        name: 'authorize'
      }
    ]
  }
]

export const asyncRoutes = [
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// 全局路由守卫：控制授权过期时的访问权限
router.beforeEach((to, from, next) => {
  const licenseValid = store.state.user.licenseValid
  const isLoginPage = to.path === '/login'
  const isAuthorizePage = to.path === '/authorize'

  // 已登录状态下检查授权
  if (store.state.user.token) {
    // 授权过期且不在授权页：强制跳转授权页
    if (licenseValid === false && !isAuthorizePage) {
      next('/authorize')
    } else {
      next()
    }
  } else {
    // 未登录状态：只允许访问登录页
    isLoginPage ? next() : next('/login')
  }
})

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router